package com.tarena.lbs.message.service.impl;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.BasePageQuery;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.utils.PageResultBuilder;
import com.tarena.lbs.message.pojo.po.MessageSearch;
import com.tarena.lbs.message.pojo.query.MessageQuery;
import com.tarena.lbs.message.pojo.vo.MessageVO;
import com.tarena.lbs.message.dao.repository.MessageESRepository;
import com.tarena.lbs.message.service.MessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class MessageServiceImpl implements MessageService {
    private final MessageESRepository messageESRepository;

    @Override
    public PageResult<MessageVO> selectNewMessages(MessageQuery query) {
        Pageable pageable = PageRequest.of(query.getPageNo()-1, query.getPageSize());
        Page<MessageSearch> list = messageESRepository.findAll(pageable);
        List<MessageVO> result = list.map(userMessageSearch -> {
            MessageVO userMessageVO = new MessageVO();
            BeanUtils.copyProperties(userMessageSearch,userMessageVO);
            return userMessageVO;
        }).stream().filter(
                userMessageVO -> userMessageVO.getStatus() == 1
                        &&userMessageVO.getType().equals(query.getType())
                        &&userMessageVO.getUserId().equals(query.getUserId()))
                .toList();
        return PageResultBuilder.build(result);
    }
    @Override
    public PageResult<MessageVO> selectAllMessages() {
        BasePageQuery query = new BasePageQuery();
        Pageable pageable = PageRequest.of(query.getPageNo()-1, query.getPageSize());
        Page<MessageSearch> list = messageESRepository.findAll(pageable);
        List<MessageVO> result = list.map(userMessageSearch -> {
            MessageVO userMessageVO = new MessageVO();
            BeanUtils.copyProperties(userMessageSearch,userMessageVO);
            return userMessageVO;
        }).stream().toList();
        return PageResultBuilder.build(result);
    }
    @Override
    public void deleteById(Integer id) {
        messageESRepository.deleteById(id);
    }
    @Override
    public void updateMsgStatus(Integer id) throws BusinessException{
        Optional<MessageSearch> messageSearch = messageESRepository.findById(id);
        if (messageSearch.isPresent()) {
            MessageSearch currentMsg = messageSearch.get();
            currentMsg.setStatus(2);
            messageESRepository.deleteById(id);
            messageESRepository.save(currentMsg);
        }else {
            throw new BusinessException("-2","该消息不存在");
        }
    }
}
